package com.activenetwork.smlite.testcase.events.createEvents.editEvents;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.schema.testcase.events.createevents.editevents.editevents002.EditEvents002DataType;
import com.activenetwork.smlite.business.EventBusiness;
import com.activenetwork.smlite.page.event.EventDetailView.EventBasicSubview;
import com.activenetwork.smlite.page.event.EventDetailView.EventSourceSubview;
import com.activenetwork.smlite.page.event.EventDetailView.EventSummarySubview;
import com.activenetwork.smlite.page.generic.ModelPage;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.jaxb.ScriptDataBindHelper;
import com.activenetwork.util.verification.gson.FailureAssertion;
import com.activenetwork.util.verification.gson.FailurePoint;

public class EditEvents002 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(EditEvents002.class);
	private EditEvents002DataType data;

	@BeforeClass(alwaysRun = true)
	public void dataSetup() {
		try {
			ScriptDataBindHelper helper = new ScriptDataBindHelper(EditEvents002.class);
			data = helper.bind(EditEvents002DataType.class);
		} catch (IOException e) {
			throw new RuntimeException("Unable to bind the script data for " + this.getClass().getName());
		}
	}

	@Test
	public void Verify_event_title_can_be_edit_successfully() {
		loginBusiness.login();

		String suffix = "_Modified";
		String originName = data.getEventInfo().getBasic().getTitle();
		String modifiedName = data.getEventInfo().getBasic().getTitle() + suffix;

		logger.info("Delete the original event name if modification failed");
		EventBusiness eventBusiness = new EventBusiness();
		try {
			eventBusiness.deleteEvent(originName, data.getEventInfo());
		} catch (RuntimeException e) {
			logger.info("The event " + originName + " does not exist!");
		}

		logger.info("Delete the modified event name");
		try {
			eventBusiness.deleteEvent(modifiedName, modifiedName);
		} catch (RuntimeException e) {
			logger.info("The event " + modifiedName + " does not exist!");
		}

		logger.info("Create a new event with location first.");
		eventBusiness.addEventUI(data.getEventInfo());
		PageLoader.load(ModelPage.class).events();
		eventBusiness.searchEvent(data.getEventInfo().getBasic().getTitle(), data.getEventInfo());
		EventSummarySubview eventSummarySubview = PageLoader.load(EventSummarySubview.class);
		EventBasicSubview eventBasicSubview = eventSummarySubview.showBasic();
		eventBasicSubview.setEventTitle(modifiedName);
		EventSourceSubview eventSourceSubview = eventBasicSubview.nextStep();
		eventBasicSubview = eventSourceSubview.goBackBasicSubview();
		String titleName = eventBasicSubview.getEventTitle();

		if (!StringUtils.equals(modifiedName, titleName)) {
			EnvContext.failurePointTL.get().add(new FailurePoint(EditEvents002.class.getName(), "Verify event title can be edited", "Event title name can not be modified."));
		}

		loginBusiness.logout();
		FailureAssertion.pop(EditEvents002.class);
	}

}
